package cn.itcast.controller;

import cn.itcast.domain.User;
import cn.itcast.service.UserService;
import cn.itcast.utils.JwtUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@Controller
@RequestMapping("/user")
public class UserController {
  @Autowired
  private UserService userService;
  private String jsonuser;

  /**
   * 登录验证
   *
   * @param user    用户
   * @param request 请求
   * @return 用户信息
   */
  @ResponseBody
  @RequestMapping(value = "/loginUser", method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
  public String loginUser(User user, HttpServletRequest request) {

    Map<String, Object> map = new HashMap<>();
    user = userService.loginUser(user.getPhone(), user.getPassword());
    HttpSession session = request.getSession(true);
    if (user != null) {
      session.setAttribute("user", user);
      String token = JwtUtil.sign(user.getPhone(), user.getUsername(), user.getAdress());
      map.put("status", 0);
      map.put("message", "登陆成功");
      map.put("token", token);

    } else {

      map.put("status", 1);
      map.put("message", "手机号或密码错误！");

    }
    jsonuser = JSON.toJSONString(map);
    return jsonuser;

  }

  /**
   * 验证登录
   *
   * @param token 加密
   * @param request 请求
   * @return 成功信息
   */
  @ResponseBody
  @RequestMapping(value = "/checkLogin", method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
  public String checkLogin(String token, HttpServletRequest request) {
    String phone = JwtUtil.verifyUser(token);
    User user = userService.checkLogin(phone);
    Map<String, Object> map = new HashMap<>();
    HttpSession session = request.getSession(true);
    if (user != null) {
      session.setAttribute("user", user);

      map.put("status", 0);
      map.put("message", "验证成功");

    } else {

      map.put("status", 1);
      map.put("message", "验证失败！");

    }
    jsonuser = JSON.toJSONString(map);
    return jsonuser;
  }


  /**
   * 用户注册
   *
   * @param user 用户
   * @param request 请求
   * @return 成功信息与用户基本信息加密
   */
  @ResponseBody
  @RequestMapping(value = "/register", method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
  public String RegisterUser(User user, HttpServletRequest request) {

    User u = userService.findRegister(user.getPhone());
    Map<String, Object> map = new HashMap<>();
    HttpSession session = request.getSession(true);
    if (u == null) {
      session.setAttribute("user",user);
      userService.RegisterUser(user);

      String token = JwtUtil.sign(user.getPhone(), user.getUsername(), user.getAdress());
      map.put("status", 0);
      map.put("message", "注册成功！");
      map.put("token", token);
    } else {
      map.put("status", 1);
      map.put("message", "手机号已经注册过！");
    }
    jsonuser = JSON.toJSONString(map);
    return jsonuser;

  }


  /**
   * 修改用户信息
   *
   * @param user 用户
   * @param request 请求
   * @return 更新成功信息
   */
  @ResponseBody
  @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "text/plain;charset=utf-8")
  public String UpdateUser(User user, HttpServletRequest request){
//    User u = userService.findUpdUser(user.getPhone());
    Map<String, Object> map = new HashMap<>();
    HttpSession session = request.getSession(true);
    session.setAttribute("user",user);
    userService.updUser(user);
    map.put("status", 0);
    map.put("message", "更新成功！");
    jsonuser = JSON.toJSONString(map);
    return jsonuser;

  }

}

